package com.knife.interfaces.helper.impl;

import com.knife.interfaces.entity.KnifeInterfaceLogRecord;
import com.knife.interfaces.helper.KnifeInterfaceLogHelper;
import org.springframework.http.HttpStatus;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import javax.validation.constraints.NotNull;
import java.util.Map;

/**
 * AOP进行方法日志监控的实现方式，和{@link KnifeAopInterfaceLogHelperImpl}不同，当前只是做内部方法的记录
 * 和上面不同的是：不会记录外部请求的url等信息
 * 2020-08-25
 *
 * @author tianhao.luo@hand-china.com
 */
public class KnifeAopMethodLogHelperImpl extends KnifeInterfaceLogHelper {
    /**
     * aop进行日志处理时,需要外部告诉他hpms系统响应成功还是失败
     */
    private final HttpStatus httpStatus;


    public KnifeAopMethodLogHelperImpl(KnifeInterfaceLogRecord knifeInterfaceLogRecord, @NotNull HttpStatus httpStatus) {
        super(knifeInterfaceLogRecord);
        Assert.notNull(httpStatus, "不可以传入空的httpStatus");
        this.httpStatus = httpStatus;
    }

    public KnifeAopMethodLogHelperImpl(KnifeInterfaceLogRecord knifeInterfaceLogRecord, @NotNull HttpStatus httpStatus, Exception exception) {
        super(knifeInterfaceLogRecord, exception);
        Assert.notNull(httpStatus, "不可以传入空的httpStatus");
        this.httpStatus = httpStatus;
    }

    @Override
    public void buildInterfaceLogResponse(Object responseObj, Class<?>... ignoreClassArray) {
        super.buildInterfaceLogResponse(responseObj,ignoreClassArray);
        // 更新状态
        knifeInterfaceLogRecord.setInterfaceResponseStatus(httpStatus.getReasonPhrase());
        knifeInterfaceLogRecord.setInterfaceResponseCode(String.valueOf(httpStatus.value()));
    }

    @Override
    public void buildInterfaceLogRequest(Object[] args, Class<?>... ignoreClassArray) {

    }

    @Override
    public void buildInterfaceLogRequest(Map<String, Object> map, Class<?>... ignoreClassArray) {
        // 如果ignoreClassList不为空,需要过滤请求的args
        if (!CollectionUtils.isEmpty(map)) {
            // 组装处理requestBody请求体
            super.setRequestBodyContentOrFileId(map);
        }
    }
}
